package com.debby.test.fish;

import com.debby.xpsoft.ModifyDataUtil;
import com.wiyun.offer.WiOffer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

public class ChangePropActivity extends Activity {
	
	TextView tv_starfish;
	TextView tv_fishhook;
	TextView tv_gun;
	int num_starfish = 0;
	int num_fishhook = 0;
	int num_gun = 0;
	int mCoins = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCoins = getIntent().getIntExtra("mCoins", 0);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.change_prop);
		tv_starfish = (TextView) findViewById(R.id.tv_starfish_number);
		tv_fishhook = (TextView) findViewById(R.id.tv_fishhook_number);
		tv_gun = (TextView) findViewById(R.id.tv_gun_number);
	}

	public void clickButton(View v){
		switch (v.getId()) {
		case R.id.ib_starfish_left:
			num_starfish = Integer.parseInt(tv_starfish.getText().toString());
			if(num_starfish<=0){
				return;
			}else{
				num_starfish--;
				tv_starfish.setText(num_starfish+"");
			}
			break;
		case R.id.ib_starfish_right:
			num_starfish = Integer.parseInt(tv_starfish.getText().toString());
				num_starfish++;
				tv_starfish.setText(num_starfish+"");
			break;
		case R.id.ib_fishhook_left:
			num_fishhook = Integer.parseInt(tv_fishhook.getText().toString());
			if(num_fishhook<=0){
				return;
			}else{
				num_fishhook--;
				tv_fishhook.setText(num_fishhook+"");
			}
			break;
		case R.id.ib_fishhook_right:
			num_fishhook = Integer.parseInt(tv_fishhook.getText().toString());
			num_fishhook++;
			tv_fishhook.setText(num_fishhook+"");
			break;
		case R.id.ib_gun_left:
			num_gun = Integer.parseInt(tv_gun.getText().toString());
			if(num_gun<=0){
				return;
			}else{
				num_gun--;
				tv_gun.setText(num_gun+"");
			}
			break;
		case R.id.ib_gun_right:
			num_gun = Integer.parseInt(tv_gun.getText().toString());
			num_gun++;
			tv_gun.setText(num_gun+"");
			break;
		case R.id.change:
			final int count = num_starfish*50+num_fishhook*100+num_gun*100;
			if(count<=0){
				DialogUtil.showErrorDialog(this, "请先选择道具的数量。", R.string.yes, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
//						Toast.makeText(ChangePropActivity.this, "兑换失败！", 0).show();
					}
				});
				return;
			}
			if(FishfreeActivity.mCoins<count){
				DialogUtil.showErrorDialog(this, "你的金币不足，赶紧去赚哦！", R.string.yes, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
//						Toast.makeText(ChangePropActivity.this, "兑换失败！", 0).show();
					}
				});
			}else{
				DialogUtil.showAlertDialog(this, "需要消耗"+count+"金币，确认兑换吗？", R.string.yes, R.string.no, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {

				    	try {
				    		ModifyDataUtil.aaa();
							ModifyDataUtil.ddd(openDatabase(),num_starfish,num_fishhook,num_gun);
						} catch (Exception e) {
							Toast.makeText(getApplicationContext(), "操作失败！无法获得Root权限或者返回重试", Toast.LENGTH_LONG).show();
							Intent intent = new Intent();
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					    	intent.setComponent(new ComponentName("com.sg.android.fish","com.sg.android.fish.FishActivity"));
					    	getApplicationContext().startActivity(intent);
							e.printStackTrace();
							return ;
						}
				    	ModifyDataUtil.ccc();
				    	//使用的金币;
				    	WiOffer.useCoins(count);
				    	FishfreeActivity.mCoins = FishfreeActivity.mCoins-count;
				    	Intent intent = new Intent();
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				    	intent.setComponent(new ComponentName("com.sg.android.fish","com.sg.android.fish.FishActivity"));
				    	getApplicationContext().startActivity(intent);
						Toast.makeText(ChangePropActivity.this, "兑换成功！", 0).show();
					}
				}, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
			}
			break;
		case R.id.back:
			finish();
			break;
		
		default:
			break;
		}
	}
	
	private final String DATABASE_PATH = android.os.Environment
			.getExternalStorageDirectory().getAbsolutePath() + "";
	private final String DATABASE_FILENAME = "temp";
	SQLiteDatabase database;
	
	private SQLiteDatabase openDatabase() {
		try {
			// 获得dictionary.db文件的绝对路径
			String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
//			File dir = new File(DATABASE_PATH);
//			// 如果/sdcard/dictionary目录中存在，创建这个目录
////			if (!dir.exists())
////				dir.mkdir();
//			// 如果在/sdcard/dictionary目录中不存在
//			// dictionary.db文件，则从res\raw目录中复制这个文件到
//			// SD卡的目录（/sdcard/dictionary）
//			if (!(new File(databaseFilename)).exists()) {
//				// 获得封装dictionary.db文件的InputStream对象
//				InputStream is = null;// getResources().openRawResource(R.raw.dictionary);
//				FileOutputStream fos = new FileOutputStream(databaseFilename);
//				byte[] buffer = new byte[8192];
//				int count = 0;
//				// 开始复制dictionary.db文件
//				while ((count = is.read(buffer)) > 0) {
//					fos.write(buffer, 0, count);
//				}
//
//				fos.close();
//				is.close();
//			}
			// 打开/sdcard/dictionary目录中的dictionary.db文件
			SQLiteDatabase database = this.openOrCreateDatabase(databaseFilename,   
                    Activity.MODE_WORLD_WRITEABLE, null);   

//			 = SQLiteDatabase.openOrCreateDatabase(
//					databaseFilename,Activity.MODE_PRIVATE, null);
			return database;
		} catch (Exception e) {
		}
		return null;
	}
}
